package cn.itcast.hystrix;

import cn.itcast.entity.User;
import cn.itcast.feign.UserFeignClient;
import cn.itcast.feign.UserFeignClientFactory;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


/**
 * @Author: zhaojie
 * @Date: Created in 11:56 2018/7/25
 * @Description: 使用fallbackFactory 添加断路器操作
 */
@Component
public class HystrixClientFallbackFactory implements FallbackFactory<UserFeignClient> {
    private static final Logger LOGGER = LoggerFactory.getLogger(HystrixClientFallbackFactory.class);
    @Override
    public UserFeignClient create(Throwable throwable) {
        if(throwable!=null){
        HystrixClientFallbackFactory.LOGGER.info("fallback; reason was: {}", throwable.getMessage());
        }
        return new UserFeignClientFactory() {
            @Override
            public User get(Long id) {
                User user = new User();
                user.setId(-1L);
                return user;
            }
        };
    /*
        return id -> {
            User user=new User();
            user.setId(id);
            user.setName(id.toString());
            return user;
        };*/
    }
}
